package jmetal.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SingletonReferenceVector {
	private static double[][] vectors;
	private static int numberOfObjectives = 0;
	
	private SingletonReferenceVector(){}
	
	/**
	 * 读取参考向量
	 * 
	 * @param objNum
	 * @param populationSize
	 * @return
	 */
	public static double[][] getReferenceVectors(int objNum, int populationSize) {
		if (vectors == null||objNum!=numberOfObjectives) {
			vectors = new double[populationSize][objNum];

			String dataFileName;
			dataFileName = "input_data/moead_weight/"+"W" + objNum + "D_" + populationSize + ".dat";

			try {
				// Open the file
				FileInputStream fis = new FileInputStream(dataFileName);
				InputStreamReader isr = new InputStreamReader(fis);
				BufferedReader br = new BufferedReader(isr);

				int i = 0;
				int j = 0;
				String aux = br.readLine();
				while (aux != null) {
					StringTokenizer st = new StringTokenizer(aux);
					j = 0;
					// numberOfObjectives = st.countTokens();
					while (st.hasMoreTokens()) {
						double value = (new Double(st.nextToken())).doubleValue();
						vectors[i][j] = value;
						// System.out.println("lambda["+i+","+j+"] = " + value)
						// ;
						j++;
					}
					aux = br.readLine();
					i++;
				}
				br.close();
			} catch (Exception e) {
				System.out.println("DirFitness: failed when reading for file: " + "/" + dataFileName);
				e.printStackTrace();
			}
		}

		return vectors;
	}
}
